package com.minglead.springboot.platform.sys.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.minglead.springboot.common.utils.CacheUtils;
import com.minglead.springboot.common.utils.SpringContextHolder;
import com.minglead.springboot.platform.sys.dao.AreaDao;
import com.minglead.springboot.platform.sys.dao.DictDao;
import com.minglead.springboot.platform.sys.entity.Area;
import com.minglead.springboot.platform.sys.entity.Dict;

/**
 * 字典工具类
 */
public class DictUtils {
	
	private static DictDao dictDao = SpringContextHolder.getBean(DictDao.class);

	public static final String CACHE_DICT_MAP = "dictMap";
	
	public static String getDictLabel(String value, String type, String defaultValue){
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)){
			for (Dict dict : getDictList(type)){
				if (type.equals(dict.getType()) && value.equals(dict.getValue())){
					return dict.getLabel();
				}
			}
		}
		return defaultValue;
	}
	
	public static String getDictLabels(String values, String type, String defaultValue){
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(values)){
			List<String> valueList = new ArrayList<String>();
			for (String value : StringUtils.split(values, ",")){
				valueList.add(getDictLabel(value, type, defaultValue));
			}
			return StringUtils.join(valueList, ",");
		}
		return defaultValue;
	}

	public static String getDictValue(String label, String type, String defaultLabel){
/*		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(label)){
			Dict dict = dictDao.getEntityByTypeAndLabel(type, label);
			if(dict!=null){
				return dict.getValue();
			}
		}*/
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(label)){
		for (Dict dict : getDictList(type)){
			if (type.equals(dict.getType()) && label.equals(dict.getLabel())){
				return dict.getValue();
			}
		}	
		}
		return defaultLabel;
	}
	
	/**
	 * 获取字典的别名，如果别名字段为空则获取label
	 * @param value
	 * @param type
	 * @param defaultLabel
	 * @return
	 */
	public static String getDictAlias(String value, String type, String defaultAlias){
		/*if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)){
			Dict dict = dictDao.getEntityByTypeAndValue(type, value);
			if(dict!=null){
				return (dict.getAlias()==null||"".equals(dict.getAlias()))?dict.getLabel():dict.getAlias();
			}
		}*/
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)){
			for (Dict dict : getDictList(type)){
				if (type.equals(dict.getType()) && value.equals(dict.getValue())){
					return (dict.getAlias()==null||"".equals(dict.getAlias()))?dict.getLabel():dict.getAlias();
				}
			}
		}
		return defaultAlias;
	}
	
	public static List<Dict> getDictList(String type){
		@SuppressWarnings("unchecked")
		Map<String, List<Dict>> dictMap = (Map<String, List<Dict>>)CacheUtils.get(CACHE_DICT_MAP);
		if (dictMap==null){
			dictMap = new HashMap<String, List<Dict>>();
			for (Dict dict : dictDao.findAllList(new Dict())){
				List<Dict> dictList = dictMap.get(dict.getType());
				if (dictList != null){
					dictList.add(dict);
				}else{
					List<Dict> list0 = new ArrayList<Dict>();
					list0.add(dict);
					dictMap.put(dict.getType(), list0);
				}
			}
			CacheUtils.put(CACHE_DICT_MAP, dictMap);
		}
		List<Dict> dictList = dictMap.get(type);
		if (dictList == null){
			dictList = new ArrayList<Dict>();
		}
		return dictList;
	}
	
	/**
	 * 自动在前边加上"--请选择--"
	 * @param type
	 * @return
	 */
	public static List<Dict> getDictListWithBlank(String type){
		List<Dict> dictList = DictUtils.getDictList(type);		
		List<Dict> newDictList = new ArrayList<Dict>();			
		//label --value
		//额外添加--请选择--
		Dict temp = new Dict();
		temp.setLabel("--请选择--");
		temp.setValue("");
		newDictList.add(0, temp);
		newDictList.addAll(dictList);		
		return newDictList;
	}
	
	
	
	/**
	 * 返回字典列表（JSON）
	 * @param type
	 * @return
	 */
	public static String getDictListJson(String type){
/*		return JsonMapper.toJsonString(getDictList(type));*/
		return getDictList(type).toString();//有待完善
	}
	
	public static String getTypeList(String type){
		String typeStr=":;";
		List<Dict> typeList=getDictList(type);
		for(int i=0;i<typeList.size();i++){
			typeStr+=typeList.get(i).getValue()+":"+typeList.get(i).getLabel()+";";
		}
		return StringUtils.substringBeforeLast(typeStr,";");
	}
	
}
